package com.fare.taluo.auth.domain.repository.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.fare.taluo.auth.domain.UserInfoEntity;
import com.fare.taluo.auth.domain.repository.IUserInfoRepository;
import com.fare.taluo.base.ddd.repository.EntityHibernateRepository;
@Repository
public class UserInfoRepositryImpl extends EntityHibernateRepository implements
		IUserInfoRepository {

	@Override
	public UserInfoEntity getUserInfoByLogonid(String logonId) {
		String hql = "from UserInfoEntity u where u.loginId=?";
		Session session = super.getSessionFactory().getCurrentSession();
		return (UserInfoEntity)session.createQuery(hql).setString(0, logonId).setMaxResults(1).uniqueResult();
	}

	@Override
	public boolean queryIsExist(Long ids, String email, String username) {
		StringBuffer sbf=new StringBuffer("select * from sys_userinfo u where 1=1");
		if(ids !=null && !ids.equals("") ){
			sbf.append(" and u.id="+ids);
		}
		if(email !=null && !email.equals("")){
			sbf.append("  and u.email ='"+email+"'");
		}
		if(username !=null && !username.equals("")){
			sbf.append(" and u.loginId= '"+username+"'");
		}
		Session session = super.getSessionFactory().getCurrentSession();
		Query query = session.createSQLQuery(sbf.toString());
		List<UserInfoEntity> list=(List<UserInfoEntity>) query.list();
		if(list.size()>0){
			return true;
		}else{
			return false;
		}
		
	}

	@Override
	public void saveOrUpate(UserInfoEntity entity) {
		Session session = super.getSessionFactory().getCurrentSession();
		
		if (null == entity.getId() || entity.getId() == 0L) {
			session.save(entity);
		}
		if (null != entity.getId() && entity.getId() != 0L) {
			session.update(entity);
		}
		
		
	}
	


	
}
